using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using service;
using Iservice;
using Demo.Net.Model;
using Utils;
namespace Demo.Net.BlogAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class UserInfoController : ControllerBase
    {
        private readonly IBaseService<Owner> _userinfo;

        public UserInfoController(IBaseService<Owner> userinfo)
        {
            this._userinfo = userinfo;
        }
        //登录验证
        [HttpGet]
        public async Task<ActionResult<ApiReturn>> Login(string phone, string pwd)
        {
            var res = await _userinfo.Find(u => u.Phone == phone && u.Password == pwd);
            if (res.Count == 0)
            {
                return ApiRetrunsHelper.Failed("电话和密码输入错误");
            }
            return ApiRetrunsHelper.Success(res);
        }
        // 查询所有反馈信息
        [HttpGet]
        public async Task<List<Owner>> FindAll()
        {
            return await _userinfo.FindAll();
        }
        // 根据搜索条件查询反馈信息
        [HttpGet]
        public async Task<List<Owner>> FindAllByCondition(string searchTerm)
        {
            return await _userinfo.FindAllByCondition(
                s => s.Id.ToString() == searchTerm
                || s.Name.Contains(searchTerm)
                || s.Sex.Contains(searchTerm)
                || s.IdNumber.Contains(searchTerm)
                || s.Phone.Contains(searchTerm)
                || s.Home.Contains(searchTerm)
                || s.License.Contains(searchTerm)
                || s.Room.ToString().Contains(searchTerm)
                );
        }
        //根据id查找
        [HttpGet]
        public async Task<List<Owner>> Find(int id)
        {
            return await _userinfo.Find(s => s.Id == id);
        }
        //添加
        [HttpPost]
        public async Task<bool> Add(Owner userInfos)
        {
            return await _userinfo.Add(userInfos);
        }
        //编辑
        // 编辑
        [HttpPut("{id}")]
        public async Task<ActionResult<ApiReturn>> Update(int id, Owner updatedOwner)
        {
            var existingOwner = await _userinfo.Find(s => s.Id == id);
            if (existingOwner == null || existingOwner.Count == 0)
            {
                return ApiRetrunsHelper.Failed("没有找到该业主信息");
            }

            // 使用 updatedOwner 的值更新 existingOwner 的属性
            existingOwner[0].Name = updatedOwner.Name;
            existingOwner[0].Sex = updatedOwner.Sex;
            existingOwner[0].IdNumber = updatedOwner.IdNumber;
            existingOwner[0].Phone = updatedOwner.Phone;
            existingOwner[0].Password = updatedOwner.Password;
            existingOwner[0].Home = updatedOwner.Home;
            existingOwner[0].License = updatedOwner.License;
            existingOwner[0].Room = updatedOwner.Room;

            var success = await _userinfo.UpdateAsync(existingOwner[0]);
            if (!success)
            {
                return ApiRetrunsHelper.Failed("更新业主信息失败");
            }

            return ApiRetrunsHelper.Success(existingOwner[0]);
        }

        //删除
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiReturn>> Delect(int id)
        {
            var result = await _userinfo.Delete(id);
            if (result)
            {
                return ApiRetrunsHelper.Success(result);
            }
            else
            {
                return ApiRetrunsHelper.Failed("没有数据");
            }

        }
    }
}